var app = getApp()
const toast = require('../../utils/toast.js')
var landlord = []
var shrinkLandlord = []
var roomTitle = []
var shrinkRoomtitle = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    condition: true,
    baiduVisible: false,
    landlordShrink: '展开',
    roomShrink: '展开',
    baiduVisible: true,
    landLordVisible: true,
    landLordState: false,
    roomInoVisible: true,
    roomInfoState: false,
  },
  toast: toast,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**  
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindBaidu: function(e) {
    var inputValue = e.detail.value;
    console.log(inputValue)
    var that = this;
    if (inputValue && typeof inputValue != 'undefined') {
      this.connectNetForData(inputValue);
    } else {
      that.setData({
        baiduVisible: true,
        landLordVisible: true,
        roomInoVisible: true,
        baiduList: [],
        landlordList: [],
        roomInfoList: []
      })
    }

  },

  connectNetForData(inputValue) {
    var that = this;
    console.log(inputValue)
    console.log(app.baseNet + "appPullNewActivity/baiduQuery?query=" + inputValue)
    wx.request({
      url: app.baseNet + "appPullNewActivity/baiduQuery?query=" + encodeURI(inputValue),
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        console.log(res.statusCode)
        if (res.statusCode == 200) {
          var search = res.data.lendReco;
          var baidu = search.baiduCoordinate;
          that.landlord = search.landlord;
          that.roomTitle = search.roomInfoTitle;
          if (baidu.length == 0 && that.landlord.length == 0 && that.roomTitle.length == 0) {
            that.setData({
              condition: false
            })
          } else {
            that.setData({
              condition: true
            })
            if (baidu != null && baidu.length > 0) {
              that.setData({
                baiduVisible: false,
                baiduList: baidu,
                condition: true
              })
            } else {
              that.setData({
                baiduVisible: true
              })
            }
            if (that.landlord != null && that.landlord.length > 0) {
              console.log(that.landlord)
              that.shrinkLandlord = new Array(2)
              if (that.landlord.length > 2) {
                that.data.landLordState = true
                for (var i = 0; i < 2; i++) {
                  var dd = that.landlord[i]
                  that.shrinkLandlord[i] = dd
                }
              } else {
                that.shrinkLandlord = that.landlord
              }

              that.setData({
                landLordVisible: false,
                landlordList: that.shrinkLandlord
              })
            } else {
              that.setData({
                landLordVisible: true,
                landlordList: []
              })
            }

            if (that.roomTitle != null && that.roomTitle.length > 0) {
              console.log(that.roomTitle)
              that.shrinkRoomtitle = new Array(2)
              if (that.roomTitle.length > 2) {
                that.data.roomInfoState = false
                for (var i = 0; i < 2; i++) {
                  var dd = that.roomTitle[i]
                  that.shrinkRoomtitle[i] = dd
                }
                console.log(that.shrinkRoomtitle)
              } else {
                that.shrinkRoomtitle = that.roomTitle
              }

              that.setData({
                roomInoVisible: false,
                roomInfoList: that.shrinkRoomtitle
              })
            } else {
              that.setData({
                roomInoVisible: true,
                roomInfoList: []
              })
            }

          }

        } else {
          toast.showSystemAbnormalToast()
        }

      }
    })
  },

  clear: function(e) {
    console.log(e)
    this.setData({
      search_input: "",
      baiduVisible: true,
      landLordVisible: true,
      roomInoVisible: true,
      baiduList: [],
      landlordList: [],
      roomInfoList: []
    })
  },
  clickLandlord: function(e) {
    var that = this
    if (that.landlord.length > 2) {
      if (that.data.landLordState) { //展开
        that.setData({
          landLordVisible: false,
          landlordList: that.landlord,
          landLordState: !that.data.landLordState,
          landlordShrink: '收缩'
        })
      } else { //收缩
        that.setData({
          landLordVisible: false,
          landlordList: that.shrinkLandlord,
          landLordState: !that.data.landLordState,
          landlordShrink: '展开'
        })
      }
    }
  },
  clickRoomInfo: function(e) {
    var that = this
    if (that.roomTitle.length > 2) {
      if (that.data.roomInfoState) { //展开
        that.setData({
          roomInoVisible: false,
          roomInfoList: that.roomTitle,
          roomInfoState: !that.data.roomInfoState,
          roomShrink: '收缩'
        })
      } else { //收缩
        that.setData({
          roomInoVisible: false,
          roomInfoList: that.shrinkRoomtitle,
          roomInfoState: !that.data.roomInfoState,
          roomShrink: '展开'
        })
      }
    }
  },
  skipToRoomList: function(e) {
    console.log('Baidu_Skip:', e)
    var index = e.currentTarget.dataset.id
    var data = this.data.baiduList[index]
    var citys = data.cityDistrict.split('-')
    var city = ''
    if (citys[0].indexOf('市')) {
      city = citys[0].split('市')[0]
    } else {
      city = citys[0]
    }
    var latitude = data.lat
    var longtitude = data.lng
    var des = data.name
    wx.navigateTo({
      url: '../roomlist/roomlist?destination=' + des + '&latitude=' + latitude + '&longitude=' + longtitude + '&cityName=' + city,
    })
  },
  skipToLandlord: function(e) {
    console.log('Landlord_Skip:', e)
    var index = e.currentTarget.dataset.id
    var data = this.landlord[index]
    var sliId = data.sliId
    wx.navigateTo({
      url: '../landlord/landlord?sliId=' + sliId,
    })
  },
  skipToRoomDetail: function(e) {
    console.log('RoomDetail_Skip:', e)
    var index = e.currentTarget.dataset.id
    var data = this.roomTitle[index]
    var roomTitle = data.roomTitle
    var roomId = data.sriId
    wx.navigateTo({
      url: '../details/details?roomTitle=' + roomTitle + '&roomId=' + roomId,
    })
  }

})